package com.hd.sup.modules.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.common.constant.BizConstant;
import com.hd.common.utils.DateUtil;
import com.hd.common.utils.GenUtil;
import com.hd.sup.modules.vehicle.model.VehicleOnOff;
import com.hd.sup.modules.vehicle.model.VehicleStatus;
import com.hd.sup.modules.vehicle.mapper.VehicleStatusMapper;
import com.hd.sup.modules.vehicle.service.IVehicleStatusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 车辆实时状态表 | 内存表 服务实现类
 * </p>
 *
 * @author chentl123
 * @since 2020-12-28
 */
@Slf4j
@Service
public class VehicleStatusServiceImpl extends ServiceImpl<VehicleStatusMapper, VehicleStatus> implements IVehicleStatusService {

    @Override
    public List<Map<String, Object>> getOffLineVehicleList(Page<Map<String, Object>> page, VehicleOnOff vehicleOnOff) {
        return this.baseMapper.getOffLineVehicleList(page, vehicleOnOff);
    }

    @Override
    public void syncData(VehicleStatus bo) {
        VehicleStatus db = getOne(new LambdaQueryWrapper<VehicleStatus>().eq(VehicleStatus::getPlatNo, bo.getPlatNo()).eq(VehicleStatus::getPlatNoColor, bo.getPlatNoColor()));
        if (db == null) {
            bo.setId(GenUtil.newId());
            bo.insert();
        } else {
            db.setLastLoginTime(bo.getLastLoginTime());
            db.setLastLogoutTime(bo.getLastLogoutTime());
            db.setEnterpriseNo(bo.getEnterpriseNo());
            db.setVehicleType(bo.getVehicleType());
            db.setAccessCode(bo.getAccessCode());
            db.setTransType(bo.getTransType());
            db.setOnline(bo.getOnline());
            db.setTotalMileage(bo.getTotalMileage());
            db.setDeptId(bo.getDeptId());
            db.updateById();
        }

    }

    @Override
    public void refreshVehicleStatus(VehicleStatus bo) {
        VehicleStatus db = getOne(new LambdaQueryWrapper<VehicleStatus>().eq(VehicleStatus::getPlatNo, bo.getPlatNo()).eq(VehicleStatus::getPlatNoColor, bo.getPlatNoColor()));
        if (db != null) {
            db.setLastLoginTime(bo.getLastLoginTime());
            db.setLastLogoutTime(bo.getLastLogoutTime());
            db.setOnline(bo.getOnline());
            db.updateById();
        }

    }
}
